package m_work;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");
        f.setPort(5672);
        f.setUsername("admin");
        f.setPassword("admin");
        //通道
        Channel ch = f.newConnection().createChannel();
        ch.queueDeclare("hellworld", //队列名
                false,              //是否是持久队列
                false,  //是否排他
                false,  //是否自动删除
                null);
        //回调函数
        DeliverCallback callback = new DeliverCallback() {

            @Override
            public void handle(String s, Delivery delivery) throws IOException {
                String mag=new String(delivery.getBody());
                System.out.println("收到"+mag);

                for (int i=0;i<mag.length();i++){
                    if (mag.charAt(i) =='.'){

                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
                System.out.println("信息处理结束");
            }
        };
        CancelCallback cancel = new CancelCallback(){

            @Override
            public void handle(String s) throws IOException {

            }
        };
        ch.basicConsume("hellworld", true,callback ,cancel);
    }
}
